package com.thoughtworks.recruit.test.jgraph.graph.builder;

import com.thoughtworks.recruit.test.jgraph.interfaces.DiGraph;
import com.thoughtworks.recruit.test.jgraph.interfaces.Graph;
import com.thoughtworks.recruit.test.util.GraphsUtil;

/**
 * Base class for {@link DiWeightedGraphBuilder} for extending.
 * 
 * @param <V> the graph vertex type
 * @param <E> the graph edge type
 * @param <G> type of the resulting graph
 * @param <B> type of this builder
 */
public abstract class DiWeightedGraphBuilderBase<V, E,
    G extends DiGraph<V, E> & DiGraph.WeightedGraph<V, E>,
    B extends DiWeightedGraphBuilderBase<V, E, G, B>>
    extends DiGraphBuilderBase<V, E, G, B>
{
    /**
     * Creates a builder based on {@code baseGraph}. {@code baseGraph} must be mutable.
     *
     * @param baseGraph the graph object to base building on
     */
    public DiWeightedGraphBuilderBase(G baseGraph)
    {
        super(baseGraph);
    }

    /**
     * Adds an weighted edge to the graph being built. The source and target vertices are added to
     * the graph, if not already included.
     *
     * @param source source vertex of the edge.
     * @param target target vertex of the edge.
     * @param weight weight of the edge.
     *
     * @return this builder object
     *
     * @see GraphsUtil#addEdgeWithVertices(Graph, Object, Object, double)
     */
    public B addEdge(V source, V target, double weight)
    {
        GraphsUtil.addEdgeWithVertices(this.graph, source, target, weight);
        return this.self();
    }

    /**
     * Adds the specified weighted edge to the graph being built. The source and target vertices are
     * added to the graph, if not already included.
     *
     * @param source source vertex of the edge.
     * @param target target vertex of the edge.
     * @param edge edge to be added to this graph.
     * @param weight weight of the edge.
     *
     * @return this builder object
     *
     * @see Graph#addEdge(Object, Object, Object)
     * @see DiGraph.WeightedGraph#setEdgeWeight(Object, double)
     */
    public B addEdge(V source, V target, E edge, double weight)
    {
        this.graph.addEdge(source, target, edge);
        this.graph.setEdgeWeight(edge, weight);
        return this.self();
    }
}
